import { Store } from 'iflux2';
import { List, fromJS } from 'immutable';
import NewsListActor from './actor/news-list-actor';
import NewsEditActor from './actor/news-edit-actor';
import { message } from 'antd';
import {
  getNewsInfo,
  getAdNewsInfo,
  updateInfoInNews,
  addNewsInterface,
  updateInfoInAdvancedNews,
  addAdvancedNewsInterface,
  delAdvancedNews,
  delNews
} from './webapi';

const Obj = {
  author: '',
  content: '',
  content_source_url: '',
  digest: '',
  need_open_comment: 0,
  only_fans_can_comment: 0,
  show_cover_pic: 0,
  thumb_media_id: '',
  thumb_url: '',
  title: '',
  url: ''
};

export default class AppStore extends Store {
  bindActor() {
    return [new NewsListActor(), new NewsEditActor()];
  }

  constructor(props) {
    super(props);
  }

  _changeTab = tabName => {
    this.dispatch('CHANGE_TAB', tabName);
    // this._initNewsList();
    message.loading('图文加载中...');
    setTimeout(() => this._initNewsList(), 1000);
  };

  //新建一条图文
  _addNews = () => {
    const tabs = this.state().get('tabs');
    this.transaction(() => {
      this.dispatch('ADD_NEWS');
      if (tabs === 'news') {
        this.dispatch(
          'CHANGE_NEWS',
          fromJS({
            update_time: new Date().getTime(),
            content: {
              news_item: [Obj]
            }
          })
        );
      } else {
        this.dispatch(
          'CHANGE_NEWS',
          fromJS({
            update_time: new Date().getTime(),
            articles: [Obj]
          })
        );
      }
    });
  };

  _editNews = () => {
    this.dispatch('EDIT_NEWS');
  };

  //进入编辑状态,设置当前编辑的图文,默认编辑图文内的第一条消息
  _changeNews = item => {
    this.transaction(() => {
      this.dispatch('EDIT_NEWS');
      this.dispatch('CHANGE_NEWS', item);
      this.dispatch('SET_NEWS_VALUE_INDEX', 0);
    });
  };

  _setEdited = (state = false) => {
    this.dispatch('EDITED_NEWS', state);
  };

  //修改图文中某条消息的值
  _changeNewsValue = ({ index, key, value }) => {
    const tabs = this.state().get('tabs');
    this.dispatch('CHANGE_NEWS_VALUE', { tabs, index, key, value });
  };

  //批量修改图文中某条消息的值
  _changeBatchNewsValue = ({ index, obj }) => {
    const tabs = this.state().get('tabs');
    this.transaction(() => {
      Object.keys(obj).forEach(key => {
        this.dispatch('CHANGE_NEWS_VALUE', { tabs, index, key, value: obj[key] });
      });
    });
  };

  //新增一个图文中 一条消息后，应该讲当前这条消息进入编辑状态
  _addNewsValue = item => {
    const tabs = this.state().get('tabs');
    this.transaction(() => {
      this.dispatch('ADD_NEWS_VALUE', { tabs, item });
      console.log(this.state().toJS());
      let state = this.state();
      let list = state.getIn(['curItem', 'content', 'news_item']) || state.getIn(['curItem', 'articles']);
      this.dispatch('SET_NEWS_VALUE_INDEX', list.size - 1);
    });
  };

  _deleteNews = index => {
    this.dispatch('DELETE_NEWS', index);
  };

  //修改一个图文中 某条消息进入编辑状态
  _setNewsIndex = index => {
    this.dispatch('SET_NEWS_VALUE_INDEX', index);
  };

  //初始化
  _initNewsList = (curPage = 1, page) => {
    //从别的路由跳转过来新建的
    if (page) {
      this.dispatch('CHANGE_TAB', page);
      this._addNews();
    } else {
      const tabs = this.state().get('tabs');
      if (tabs === 'news') {
        getNewsInfo({ page: curPage }).then(newsList => {
          this.transaction(() => {
            this.dispatch('SET_CURRENT_PAGE', curPage - 1);
            this.dispatch('QUIT_EDIT');
            this.dispatch('INIT_NEWS_LIST', fromJS({ curPage, newsList }));
          });
        });
      } else if (tabs === 'adNews') {
        getAdNewsInfo({ page: curPage }).then(newsList => {
          this.transaction(() => {
            this.dispatch('SET_CURRENT_PAGE', curPage - 1);
            this.dispatch('QUIT_EDIT');
            this.dispatch('INIT_NEWS_LIST', fromJS({ curPage, newsList }));
          });
        });
      }
    }
  };

  //调用api！更新图文中的某条消息
  _updateInfoInNews = ({ media_id, index, articles }) => {
    const tabs = this.state().get('tabs');
    if (tabs === 'news') {
      updateInfoInNews({ media_id, index, articles });
      return;
    }
    updateInfoInAdvancedNews({ media_id, index, articles });
  };

  //调用api！添加一条图文
  _addNewsInterface = articles => {
    const tabs = this.state().get('tabs');
    if (tabs === 'news') {
      addNewsInterface(articles);
      return;
    }
    addAdvancedNewsInterface(articles);
  };

  //调用api！删除一条图文
  _delNews = media_id => {
    const tabs = this.state().get('tabs');
    if (tabs == 'news') {
      delNews(media_id).then(({ errcode }) => {
        if (!errcode) {
          this._initNewsList();
          message.success('删除成功');
        }
      });
    } else {
      delAdvancedNews(media_id).then(({ errcode }) => {
        if (!errcode) {
          this._initNewsList();
          message.success('删除成功');
        }
      });
    }
  };
}
